package com.banmao.mall.service.admin.impl;

import com.banmao.mall.mapper.SystemMapper;
import com.banmao.mall.model.bo.admin.config.ExpressBO;
import com.banmao.mall.model.bo.admin.config.MallBO;
import com.banmao.mall.model.bo.admin.config.OrderBO;
import com.banmao.mall.model.bo.admin.config.WxBO;
import com.banmao.mall.model.pojo.System;
import com.banmao.mall.service.admin.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class ConfigServiceImpl implements ConfigService {
    @Autowired
    SystemMapper systemMapper;

    /**
     * 商场配置，对应表名cskaoyanmall_system，根据key_name，修改key_value为提交的数据
     * @param mallBO
     * @return
     */
    @Override
    public int mallUpdate(MallBO mallBO) {
        System record = new System();
        int affectRows1 = UpdateKey_value(record, mallBO.getCskaoyanmall_mall_name(), "cskaoyan_mall_mall_name");
        int affectRows2 = UpdateKey_value(record, mallBO.getCskaoyanmall_mall_address(), "cskaoyan_mall_mall_address");
        int affectRows3 = UpdateKey_value(record, mallBO.getCskaoyanmall_mall_phone(), "cskaoyan_mall_mall_phone");
        int affectRows4 = UpdateKey_value(record, mallBO.getCskaoyanmall_mall_qq(), "cskaoyan_mall_mall_qq");
        if (affectRows1 == 1 && affectRows2 == 1 && affectRows3 == 1 && affectRows4 == 1) {
            return 200;
        }
        return 500;
    }

    @Override
    public MallBO mallSelect() {
        System system1 = systemMapper.selectByKey_name("cskaoyan_mall_mall_name");
        System system2 = systemMapper.selectByKey_name("cskaoyan_mall_mall_address");
        System system3 = systemMapper.selectByKey_name("cskaoyan_mall_mall_phone");
        System system4 = systemMapper.selectByKey_name("cskaoyan_mall_mall_qq");
        MallBO mallBO = new MallBO();
        mallBO.setCskaoyanmall_mall_name(system1.getKeyValue());
        mallBO.setCskaoyanmall_mall_address(system2.getKeyValue());
        mallBO.setCskaoyanmall_mall_phone(system3.getKeyValue());
        mallBO.setCskaoyanmall_mall_qq(system4.getKeyValue());
        return mallBO;
    }

    @Override
    public ExpressBO expressSelect() {
        System system1 = systemMapper.selectByKey_name("cskaoyan_mall_express_freight_min");
        System system2 = systemMapper.selectByKey_name("cskaoyan_mall_express_freight_value");
        ExpressBO expressBO = new ExpressBO();
        expressBO.setCskaoyanmall_express_freight_min(system1.getKeyValue());
        expressBO.setCskaoyanmall_express_freight_value(system2.getKeyValue());
        return expressBO;
    }

    @Override
    public int expressUpdate(ExpressBO expressBO) {
        System record = new System();
        int affectRows1 = UpdateKey_value(record, expressBO.getCskaoyanmall_express_freight_min(), "cskaoyan_mall_express_freight_min");
        int affectRows2 = UpdateKey_value(record, expressBO.getCskaoyanmall_express_freight_value(), "cskaoyan_mall_express_freight_value");
        if (affectRows1 == 1 && affectRows2 == 1) {
            return 200;
        }
        return 500;

    }

    @Override
    public OrderBO orderSelect() {
        System system1 = systemMapper.selectByKey_name("cskaoyan_mall_order_comment");
        System system2 = systemMapper.selectByKey_name("cskaoyan_mall_order_unconfirm");
        System system3 = systemMapper.selectByKey_name("cskaoyan_mall_order_unpaid");
        OrderBO orderBO = new OrderBO();
        orderBO.setCskaoyanmall_order_comment(system1.getKeyValue());
        orderBO.setCskaoyanmall_order_unconfirm(system2.getKeyValue());
        orderBO.setCskaoyanmall_order_unpaid(system3.getKeyValue());
        return orderBO;
    }

    @Override
    public int orderUpdate(OrderBO orderBO) {
        System record = new System();
        int affectRows1 = UpdateKey_value(record, orderBO.getCskaoyanmall_order_comment(), "cskaoyan_mall_order_comment");
        int affectRows2 = UpdateKey_value(record, orderBO.getCskaoyanmall_order_unconfirm(), "cskaoyan_mall_order_unconfirm");
        int affectRows3 = UpdateKey_value(record, orderBO.getCskaoyanmall_order_unpaid(), "cskaoyan_mall_order_unpaid");

        if (affectRows1 == 1 && affectRows2 == 1 && affectRows3 == 1) {
            return 200;
        }
        return 500;
    }

    @Override
    public WxBO wxSelect() {
        System system1 = systemMapper.selectByKey_name("cskaoyan_mall_wx_catlog_goods");
        System system2 = systemMapper.selectByKey_name("cskaoyan_mall_wx_catlog_list");
        System system3 = systemMapper.selectByKey_name("cskaoyan_mall_wx_index_brand");
        System system4 = systemMapper.selectByKey_name("cskaoyan_mall_wx_index_hot");
        System system5 = systemMapper.selectByKey_name("cskaoyan_mall_wx_index_new");
        System system6 = systemMapper.selectByKey_name("cskaoyan_mall_wx_index_topic");
        System system7 = systemMapper.selectByKey_name("cskaoyan_mall_wx_share");
        WxBO wxBO = new WxBO();
        wxBO.setCskaoyanmall_wx_catlog_goods(system1.getKeyValue());
        wxBO.setCskaoyanmall_wx_catlog_list(system2.getKeyValue());
        wxBO.setCskaoyanmall_wx_index_brand(system3.getKeyValue());
        wxBO.setCskaoyanmall_wx_index_hot(system4.getKeyValue());
        wxBO.setCskaoyanmall_wx_index_new(system5.getKeyValue());
        wxBO.setCskaoyanmall_wx_index_topic(system6.getKeyValue());
        wxBO.setCskaoyanmall_wx_share(system7.getKeyValue());
        return wxBO;
    }

    @Override
    public int wxUpdate(WxBO wxBO) {
        System record = new System();
        int affectRows1 = UpdateKey_value(record, wxBO.getCskaoyanmall_wx_catlog_goods(), "cskaoyan_mall_wx_catlog_goods");
        int affectRows2 = UpdateKey_value(record, wxBO.getCskaoyanmall_wx_catlog_list(), "cskaoyan_mall_wx_catlog_list");
        int affectRows3 = UpdateKey_value(record, wxBO.getCskaoyanmall_wx_index_brand(), "cskaoyan_mall_wx_index_brand");
        int affectRows4 = UpdateKey_value(record, wxBO.getCskaoyanmall_wx_index_hot(), "cskaoyan_mall_wx_index_hot");
        int affectRows5 = UpdateKey_value(record, wxBO.getCskaoyanmall_wx_index_new(), "cskaoyan_mall_wx_index_new");
        int affectRows6 = UpdateKey_value(record, wxBO.getCskaoyanmall_wx_index_topic(), "cskaoyan_mall_wx_index_topic");
        int affectRows7 = UpdateKey_value(record, wxBO.getCskaoyanmall_wx_share(), "cskaoyan_mall_wx_share");
        if (affectRows1 == 1 && affectRows2 == 1 && affectRows3 == 1 && affectRows4 == 1 && affectRows5 == 1 && affectRows6 == 1 && affectRows7 == 1) {
            return 200;
        }
        return 500;
    }

    private int UpdateKey_value(System record ,String mallBOFieldValue, String keyNameOfDataBase) {
        record.setKeyValue(mallBOFieldValue);
        record.setKeyName(keyNameOfDataBase);
        int affectRows = systemMapper.updateByKey_nameSelective(record);
        return affectRows;
    }
}
